<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const echarts = inject('echarts');
const props = defineProps({
  barData: {
    type: Object,
    default: () => {
    }
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    const tempSerisDataLength = props?.barData?.seriesData?.length
    let tempLegendData = [
      {
        name: '未干预',
        itemStyle: {
          color: '#0E5FFF'
        }
      }
    ]
    let tempSerisData = [
      {
        data: props?.barData?.seriesData[0],
        type: 'bar',
        stack: '未干预',
        name: '未干预',
        itemStyle: {
          color: '#0E81FF'
        }
      }
    ]
    if (tempSerisDataLength === 2) {
      tempLegendData.push({
        name: '干预',
        itemStyle: {
          color: '#5ABE52'
        }
      })
      tempSerisData.push({
        data: props?.barData?.seriesData[1],
        type: 'bar',
        stack: '干预',
        name: '干预',
        itemStyle: {
          color: '#5ABE52'
        }
      })
    }
    let option = {
      barMaxWidth: pxToRem(11 * props?.dialogData?.fontSizeTimes),
      legend: {
        x: 'center',
        icon: 'rect',
        itemWidth: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        itemHeight: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        textStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes)
        },
        data: tempLegendData
      },
      grid: {
        left: '2%',
        right: '2%',
        bottom: 0,
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 20 : 15}%`,
        containLabel: true
      },
      dataZoom: { // 数据过多内部拖动逻辑
        type: "inside",
        zoomLock: true, // 不可缩放
        startValue: 0,
        endValue: props?.dialogData?.fontSizeTimes === 1 ? 7 : 6,
      },
      xAxis: {
        type: 'category',
        data: props?.barData?.xAxisData,
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false, // 不显示坐标轴线
        },
        splitLine: {
          show: false // 不显示网格线
        },
        axisLabel: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          interval: 0
        }
      },
      yAxis: [
        {
          name: props?.barData?.yAxisName,
          min: 0,
          max: (value) => value.max || props?.barData?.yAxisDefaultMaxValue,
          nameTextStyle: {
            color: 'white',
            fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
            padding: [0, 0, 0, pxToRem(props?.dialogData?.fontSizeTimes === 1 ? -30 : -20)]
          },
          axisTick: {
            show: false // 不显示坐标轴刻度线
          },
          axisLine: {
            show: false, // 不显示坐标轴线
          },
          splitLine: {
            show: false // 不显示网格线
          },
          axisLabel: {
            fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
            color: 'white'
          },
        }
      ],
      series: tempSerisData
    }
    option && myChart.value.setOption(option, true);

    myChart.value.on('legendselectChanged', (params) => {
      event.stopPropagation();
    })
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};

defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h170 {
  height: 260px;
}
.dialog-h170 {
  width: 1120px;
  height: 500px;
}
</style>